package com.chance.cc.crawler.prod.command.job.domain.news.pharmnet;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.scripts.pharmnet.PharmnetSearchCrawlerScript;
import com.chance.cc.crawler.development.scripts.pharmnet.PharmnetSiteCrawlerScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.NewsCommonScript;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * created by baixin on 2020/11/26
 * mail 279020185@qq.com
 */
public class PharmentSearchCrawlerScheduleJob extends NewsCommonScript {

    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){


        CrawlerJob jdCrawlerJob = crawlerJob();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(PharmnetSearchCrawlerScript.domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(jdCrawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("医药网关键词定时采集");
        crawlerScheduleJob.setCrawlerKey(jdCrawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());
        return jdCrawlerJob;

    }

    /**
     * taobao采集
     * @return
     */
    public static CrawlerJob crawlerJob() {

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(PharmnetSearchCrawlerScript.domain, turnPage)
                .httpUrl(PharmnetSearchCrawlerScript.startUrl)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(7*24+3,null))
                .needWashed(false)
                .needParsed(true)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addSite("search");

        CrawlerRequestRecord initNewsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest(PharmnetSiteCrawlerScript.domain,turnPageItem)
                .httpUrl("http://192.168.1.217:9599/v1/meta/pharmnet/keys?site=search")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        //初始 comment crawler request record
        String requestQueueName = StringUtils.joinWith("-","crawler",PharmnetSearchCrawlerScript.domain,"queue");

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(1)
                .triggerInfo(
                        PharmnetSearchCrawlerScript.domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",PharmnetSearchCrawlerScript.pharmnetSite,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .kafkaResultPipeline(null,kafkaTopic,null)
                .requestRecord(requestRecord)
                .supportRecord(initNewsCrawlerRecord)
                .build();
    }

}
